﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BlogDH.User
{
    public partial class ChangePwd : BasePage
    {
        protected string errPwd = string.Empty;
        protected string errRePwd = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.HttpMethod == "POST")
            {
                SavePwd();
            }
        }

        private void SavePwd()
        {
            string oldpwd = Request.Form["oldpwd"];
            string newpwd = Request.Form["newpwd"];
            string repwd = Request.Form["repwd"];

            if (newpwd != repwd)
            {
                errRePwd = "两次输入的密码不一致";
            }
            else if (newpwd.Length == 0)
            {
                errRePwd = "新密码不能为空";
            }
            else if (newpwd.Length > 20)
            {
                errRePwd = "密码超过最大长度";
            }
            else
            {
                string pwd = CommMethod.MD5Hash(newpwd);
                Model.UserInfo user = new BLL.UserInfo().GetModel(UserID, UserName);
                if (user == null)
                {
                    Response.Write("<script>window.location.href='/login.aspx'</script>");
                    Response.End();
                }
                else if (user.PWD == pwd)
                {
                    errPwd = "密码错误";
                }
                else
                {
                    bool flag = new BLL.UserInfo().Update(new Model.UserInfo() { UserID = base.UserID, UserName = base.UserName, PWD = pwd });

                    if (flag)
                    {
                        Response.Write("<script>alert('密码修改成功')</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('密码修改失败')</script>");
                    }
                }
            }
        }
    }
}